package com.moon.config;

import com.moon.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.CacheManager;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.Collection;

@Configuration
public class CacheConfig {
    @Autowired
    @Qualifier("redisTemplate") //按类型注入
    private RedisTemplate redisTemplate;

    @Bean
    public CacheManager cacheManager(){
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        RedisCache cache0 = new RedisCache();
        cache0.setName("cache0");
        cache0.setRedisTemplate(redisTemplate);
        Collection<RedisCache> coll = new ArrayList<>();
        coll.add(cache0);
        cacheManager.setCaches(coll);
        return cacheManager;
    }
}
